<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>手写new</title>
</head>

<body>
    <script>
        function create(fn, ...args) {
            if (typeof fn !== 'function') {
                throw TypeError('Error')
            }
            // 1、用new Object() 的方式新建了一个对象obj
            // var obj = new Object();
            // 2、给该对象的__proto__赋值为fn.prototype，即设置原型链
            // obj.__proto__ = fn.prototype;

            // 1、2步骤合并
            // 创建一个空对象，且这个空对象继承构造函数的 prototype 属性
            // 即实现 obj.__proto__ === constructor.prototype
            var obj = Object.create(fn.prototype);
            var res = fn.apply(obj, args);
            return res instanceof Object ? res : obj;
        }

        function Person(name) {
            this.name = name
            // return { 1: 1 }
        }
        var person = new Person('lucas')
        console.log(person)
        var person1 = create(Person, 1, 2)
        console.log(person1);

    </script>
</body>

</html>